home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 11 - 1995 / 11.03 Mar 95 / Dynamic Localization snippets < prev   
Encoding:
Text File  |  1995-01-26  |  4.5 KB  |  173 lines  |  [TEXT/R*ch]

  1. Dynamic Localization
  2.  
  3. OSErr ResFile2Resource(Str32 *fileName, short vRefNum, long dirID, short languageID);
  4. {
  5.     // see description of parameters below
  6.  
  7.     OSErr        err;
  8.     short        refNum;
  9.     long        eof;
  10.     Handle    tHandle;
  11.  
  12.     // Open the resource fork of the file for BLOCK reads
  13.     if (err = HOpenRF(vRefNum,dirID,fileName,fsRdPerm,&refNum)) goto exit:
  14.  
  15.     // Get the size of the resource fork
  16.     if (err = GetEOF(refNum, &eof)) goto exitErr;
  17.  
  18.     // Create a handle to hold the entire resource fork
  19.     tHandle = NewHandle(eof);
  20.     if (err = MemError()) goto exitErr;
  21.  
  22.     // Read the entire resource fork into our handle
  23.     if (err = FSRead(refNum, &eof, *tHandle)) goto exitErr;
  24.     
  25.     // The current resource file is our installer application
  26.     // Add this entire resource fork to our installer application
  27.     AddResource(tHandle,'Lirs',languageID,"\p");
  28.     if (err = ResError()) goto exitErr;
  29.  
  30.     WriteResource(tHandle);
  31.     err = ResError();
  32.  
  33.     // Release memory occupied by this resource
  34.     ReleaseResource(tHandle);
  35.     tHandle = nil;
  36.  
  37. exitErr:
  38.     FSClose(refNum);
  39.     if (tHandle) DisposeHandle(tHandle);
  40.  
  41. exit:
  42.     return err;
  43. }
  44.  
  45.  
  46. Language Magic
  47.  
  48. // Assume English in case something fails
  49. gLanguageCode  = 0;
  50.  
  51. // The following chunk of code was graciously provided to me by a engineer at // Adobe Systems, THANKS!
  52. // Make sure Script Manager calls are available
  53. if (TrapAvailable(_ScriptUtil)) {
  54.  
  55.     // Get the ID of the current system font and use it to find out
  56.     // the script code
  57.     scriptCode = Font2Script(GetSysFont());
  58.  
  59.     // use the script code to find out the language code for that script
  60.     gLanguageCode = GetScript(scriptCode,smScriptLang);
  61.     
  62.     // "GetScript(scriptCode, smScriptLang)" doesn't return the
  63.     // correct language code on KanjiTalk 6.0.7-J, if it returns zero and if
  64.     // the current script is not Roman, we directly get the language code
  65.     // from the itlb resource.
  66.     if (gLanguageCode == 0 && scriptCode != 0) {             
  67.         if (tHandle = GetResource('itlb', scriptCode)) {
  68.             gLanguageCode = (*(ItlbRecord **)tHandle)->itlbLang;
  69.         }
  70.     }
  71.  
  72. }
  73.  
  74. // Add 1000 to get the resource ID of localized resources
  75. gLanguageCode += 1000;
  76.  
  77.  
  78. Localizing File Names
  79.  
  80. // See if there are any localized filenames
  81. tFilesHdl = Get1Resource('Flnm', gLanguageCode);
  82. if (tFilesHdl) {
  83.     .. Our installer contains a handle of filenames, so we need to call routine to     .. replace the original names with these localized filenames.
  84.     ReleaseResource(tFIlesHdl);
  85. }
  86.  
  87. Localizing Installer Resources
  88.  
  89. // See if there are any localized resources.
  90. tHandle = Get1Resource('Lirs',gLanuageCode);
  91. if (tHandle) {
  92.     DetachResource(tHandle);
  93.  
  94.     // Store the size of the resource
  95.     count = GetHandleSize(tHandle);
  96.  
  97.     // Call routine to find a volume that is large enough to create a file the     // size of the resource.
  98.     vRefNum = FindValidVolume(count);
  99.  
  100.     if  (vRefNum) {
  101.         // Come up with a unique filename
  102.         NumToString(TickCount(),tFileName);
  103.  
  104.         // Create file to the root directory (dirID = 2)
  105.         err = HCreateResFile(vRefNum, 2, tFileName);
  106.         if (err) goto doDispose;
  107.  
  108.         // Open the Resource fork for block writing
  109.         err = HOpenRF(vRefNum, 2, tFileName, &refNum);
  110.         if (err) goto doDispose;
  111.  
  112.         // Write out resource to create the file's resource fork
  113.         err = FSWrite(refNum, &count,*tHandle);
  114.  
  115.         FSClose(refNum);
  116.         if (err) goto doDispose;
  117.  
  118.         // Open the resource file we just created
  119.         gExtraResRefNum = HOpenResFile(vRefNum, 2, tFileName, fsRdPerm);
  120.     }
  121.  
  122. doDispose:
  123.     DisposeHandle(tHandle);
  124. }
  125.  
  126.  
  127. Clean Up After Yourself
  128.  
  129. // Cleanup
  130. if (gExtraResRefNum != -1) {
  131.     // Since we don't store this filename anywhere, we need to
  132.     // call PBGetFCBInfo to get the filename.
  133.  
  134.     FCBPBRec    pb;
  135.     OSErr            err;
  136.     Str32            tStr;
  137.     
  138.     pb.ioFCBIndx = 0;            // We're not indexing
  139.     pb.ioVRefNum = 0;            // 0 = All open files
  140.     pb.ioRefNum = gExtraResRefNum;    // Refnum of our resource file
  141.     pb.ioNamePtr = tStr;            // Storage place for the name
  142.     err = PBGetFCBInfo(&pb, false);
  143.     if (!err) {
  144.         // Close the Resource File before trying to delete it
  145.         CloseResFile(gExtraResRefNum);
  146.  
  147.         // Delete the Resource File now that we have the vRefNum, directory ID,
  148.         // and the Filename
  149.         err = HDelete(pb.ioFCBVRefNum, pb.ioFCBParID, tStr);
  150.     }
  151. }
  152.  
  153.  
  154. Tipping is Not a City in China
  155.  
  156. // Copying the Resource Fork from one file to another…
  157. HOpenRF(sourceFile…    &sourceRefNum);
  158. HOpenRF(destFile…    &destRefNum);
  159. GetEOF(sourceRefNum,&sourceSize);
  160. CopyBytes(sourceRefNum,destRefNum,sourceSize);
  161. FSClose(sourceRefNum);
  162. FSClose(destRefNum);
  163.  
  164. // If you need to, then delete some resources…
  165. SetResLoad(false);
  166. destRefNum = HOpenResFile(destFile…);
  167. Get1Resource();
  168. RmveResource();
  169. CloseResFile(destRefNum);
  170. SetResLoad(true);
  171.  
  172.  
  173.